package edu.whut.mall.admin.controller;

import edu.whut.mall.admin.domain.GroupBuyCategoryParams;
import edu.whut.mall.admin.domain.GroupBuyRuleParams;
import edu.whut.mall.admin.service.IGroupBuyService;
import edu.whut.mall.admin.vo.GroupBuyCategoryVo;
import edu.whut.mall.admin.vo.GroupBuyRuleVo;
import edu.whut.mall.common.api.CommonPage;
import edu.whut.mall.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * @program: d9lab-mall
 * @description: 团购功能控制器
 * @author: Wayne
 * @create: 2020-05-15 17:53
 **/
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/groupBuy")
@RestController
@Api(value="团购管理器",tags = {"团购管理器"})
public class RestGroupBuyController {

    private final IGroupBuyService groupBuyService;

    @ApiOperation("添加团购分类")
    @PostMapping(value = "/addOneGroupBuyCategory")
    public CommonResult addOneGroupBuyCategory(@Valid @RequestBody GroupBuyCategoryParams groupBuyCategoryParams){
        int result = groupBuyService.insertOneGroupBuyCategory(groupBuyCategoryParams);
        if (result <= 0) {
            return CommonResult.failed("添加团购分类失败");
        }
        return CommonResult.success("添加团购分类成功");
    }

    @ApiOperation("更新团购分类")
    @PostMapping(value = "/updateOneGroupBuyCategory")
    public CommonResult updateOneGroupBuyCategory(@Valid @RequestBody GroupBuyCategoryParams groupBuyCategoryParams){
        int result = groupBuyService.updateOneGroupBuyCategory(groupBuyCategoryParams);
        if (result <= 0) {
            return CommonResult.failed("更新团购分类失败");
        }
        return CommonResult.success("更新团购分类成功");
    }

    @ApiOperation("更新团购分类状态")
    @PostMapping(value = "/updateOneGroupBuyCategoryStatus")
    public CommonResult updateOneGroupBuyCategoryStatus(@NotNull @RequestParam Long categoryId, @NotNull @RequestParam Integer status) {
        int result = groupBuyService.updateOneGroupBuyCategoryStatus(categoryId, status);
        if (result <= 0) {
            return CommonResult.failed("更新团购分类状态失败");
        }
        return CommonResult.success("更新团购分类状态成功");
    }
    @ApiOperation("删除团购分类")
    @PostMapping(value = "/deleteOneGroupBuyCategory")
    public CommonResult deleteOneGroupBuyCategory(@NotNull @RequestParam Long categoryId){
        int result = groupBuyService.deleteOneGroupBuyCategory(categoryId);
        if (result <= 0) {
            return CommonResult.failed("删除团购分类失败");
        }
        return CommonResult.success("删除团购分类成功");
    }

    @ApiOperation("获取所有团购分类")
    @PostMapping(value = "/listGroupBuyCategoryVos")
    public CommonResult listGroupBuyCategoryVos( @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value="size",defaultValue ="10") Integer size){
        CommonPage<GroupBuyCategoryVo> groupBuyCategoryVos = groupBuyService.listGroupBuyCategoryVos(pageNum, size);
        if (groupBuyCategoryVos == null) {
            return CommonResult.failed("获取团购分类失败或者分类不存在");
        }
        return CommonResult.success(groupBuyCategoryVos,"获取团购分类成功");
    }

    @ApiOperation("添加团购规则")
    @PostMapping(value = "/addOneGroupBuyRule")
    public CommonResult addOneGroupBuyRule(@Valid @RequestBody GroupBuyRuleParams groupBuyRuleParams){
        int result = groupBuyService.insertOneGroupBuyRule(groupBuyRuleParams);
        if (result <= 0) {
            return CommonResult.failed("添加团购规则失败");
        }
        return CommonResult.success("添加团购规则成功");
    }

    @ApiOperation("更新团购规则")
    @PostMapping(value = "/updateOneGroupBuyRule")
    public CommonResult updateOneGroupBuyRule(@RequestParam Long ruleId, @Valid @RequestBody GroupBuyRuleParams groupBuyRuleParams){
        int result = groupBuyService.updateOneGroupBuyRule(ruleId, groupBuyRuleParams);
        if (result <= 0) {
            return CommonResult.failed("更新团购规则失败");
        }
        return CommonResult.success("更新团购规则成功");
    }

    @ApiOperation("更新团购规则状态")
    @PostMapping(value = "/updateOneGroupBuyRuleStatus")
    public CommonResult updateOneGroupBuyRuleStatus(@NotNull @RequestParam Long ruleId, @NotNull @RequestParam Integer status){
        int result = groupBuyService.updateOneGroupBuyRuleStatus(ruleId, status);
        if (result <= 0) {
            return CommonResult.failed("更新团购规则状态失败");
        }
        return CommonResult.success("更新团购规则状态成功");
    }

    @ApiOperation("根据团购分类id获取团购规则")
    @GetMapping("/listGroupBuyRuleByGroupBuyCategoryId")
    public CommonResult listGroupBuyRuleByGroupBuyCategoryId(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                                                             @RequestParam Long categoryId,
                                                             @RequestParam(required = false) Long merchantId) {
        CommonPage<GroupBuyRuleVo> groupBuyRuleVos = groupBuyService.listGroupBuyRuleByGroupBuyCategoryId(categoryId, merchantId, pageNum, size);
        if (groupBuyRuleVos != null) {
            return CommonResult.success(groupBuyRuleVos, "获取团购规则成功");
        }
        return CommonResult.failed("获取团购规则失败");
    }

    @ApiOperation("删除一个团购规则")
    @PostMapping("/deleteOneGroupBuyRule")
    public CommonResult deleteOneGroupBuyRule(@RequestParam Long ruleId) {
        int result = groupBuyService.deleteOneGroupBuyRule(ruleId);
        if (result >= 0) {
            return CommonResult.success("删除团购规则成功");
        }
        return CommonResult.failed("删除团购规则失败");
    }

}
